package com.wngbms.netty.gatewaydemo.filter;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;

public class RemoveRequestHeaderFilter implements HttpRequestFilter {

    private String key;
    private HttpRequestFilter nextFilter;

    public RemoveRequestHeaderFilter() {
    }

    public RemoveRequestHeaderFilter(String key) {
        this.key = key;
    }

    public RemoveRequestHeaderFilter(HttpRequestFilter nextFilter) {
        this.nextFilter = nextFilter;
    }

    @Override
    public void filter(FullHttpRequest fullHttpRequest, ChannelHandlerContext ctx) {

        HttpHeaders headers = fullHttpRequest.headers();
        headers.remove(this.key);

        if (nextFilter != null) {
            nextFilter.filter(fullHttpRequest, ctx);
        }
    }

    public void setNextFilter(HttpRequestFilter nextFilter) {
        this.nextFilter = nextFilter;
    }
}
